<?php

namespace api\controller;


use common\service\ReviewFeedbackService;
use common\controller\VerifyController;
use common\exception\LogicException;
use common\validate\ReviewFeedback;
use support\ErrorCode;
use think\Exception;
use think\Response;
use think\Request;
use Throwable;

class ReviewFeedbackController extends VerifyController
{

    /**
     * @var ReviewFeedbackService
     */
    private $reviewFeedbackService;

    public function __construct()
    {
        parent::__construct();
        $this->reviewFeedbackService = ReviewFeedbackService::getInstance();
    }

    /**
     * 审核反馈创建
     * @param Request $request
     * @return Response
     * @throws Throwable
     * @throws Exception
     */
    public function save(Request $request)
    {
        $param = $request->all();
        $validate = new ReviewFeedback();
        $validate->scene('Create');
        if (empty($param['data']['review_feedback']['id']) && !$validate->check($param)) {
            throw_strack_exception($validate->getError(), ErrorCode::REQUEST_PARAM_ERROR);
        }

        $validate->scene('Update');
        if (!empty($param['data']['review_feedback']['id']) && !$validate->check($param)) {
            throw_strack_exception($validate->getError(), ErrorCode::REQUEST_PARAM_ERROR);
        }

        $validate->scene('SaveReviewFeedbackCosMedia');
        if (!empty($param['data']['review_feedback_cos_media'])) {
            foreach ($param['data']['review_feedback_cos_media'] as $reviewFeedbackCosMediaItem) {
                if (!$validate->check($reviewFeedbackCosMediaItem)) {
                    throw_strack_exception($validate->getError(), ErrorCode::REQUEST_PARAM_ERROR);
                }
            }
        }

        try {
            $req = $this->reviewFeedbackService->save($param['data']);
            return json(success_response('', $req));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 获取审核反馈列表
     * @param Request $request
     * @return Response
     * @throws \Exception
     */
    public function getReviewFeedbackList(Request $request)
    {
        $param = $request->all();
        $req = $this->reviewFeedbackService->getReviewFeedbackList($param["param"]);
        return json(success_response('', $req));
    }

    /**
     * 添加审核反馈视频
     * @param Request $request
     * @return Response
     * @throws Throwable
     * @throws Exception
     */
    public function addCosMedia(Request $request)
    {
        $data = $request->all()['data'];
        $req = $this->reviewFeedbackService->addCosMedia($data);
        return json(success_response('', $req));
    }

    /**
     * 删除审核反馈视频
     * @param Request $request
     * @return Response
     * @throws Throwable
     * @throws Exception
     */
    public function deleteCosMedia(Request $request)
    {
        $data = $request->all()['data'];
        $req = $this->reviewFeedbackService->deleteCosMedia($data);
        return json(success_response('', $req));
    }

    /**
     * 获取审核反馈工序分类列表
     * @param Request $request
     * @return Response
     */
    public function getReviewFeedbackStepCategoryList(Request $request)
    {
        $param = $request->all();
        $reviewFeedbackId = $param['param']["filter"]["review_feedback_id"] ?? 0;
        $moduleCode = $param['param']["filter"]["module_code"] ?? "";
        $episodeCode = $param['param']["filter"]["episode_code"] ?? "";
        $type = $param['param']["filter"]["type"] ?? "review_feedback";
        $req = $this->reviewFeedbackService->getReviewFeedbackStepCategoryList($reviewFeedbackId, $moduleCode, $episodeCode, $type);
        return json(success_response('', $req));
    }

    /**
     * 镜头审核反馈列表
     * @param Request $request
     * @return Response
     */
    public function shotReviewFeedbackList(Request $request)
    {
        $param = $request->all()['param'];
        $req = $this->reviewFeedbackService->shotReviewFeedbackList($param);
        return json(success_response('', $req));
    }

    /**
     * 资产审核反馈
     * @param Request $request
     * @return Response
     */
    public function assetReviewFeedbackList(Request $request)
    {
        $param = $request->all()['param'];
        $req = $this->reviewFeedbackService->assetReviewFeedbackList($param);
        return json(success_response('', $req));
    }

    /**
     * 获取发起返修列表
     * @param Request $request
     * @return Response
     */
    public function getSendRepairList(Request $request)
    {
        $param = $request->all()['param'];
        $req = $this->reviewFeedbackService->getSendRepairList($param);
        return json(success_response('', $req));

    }

    /**
     * 添加审核反馈返修意见
     * @param Request $request
     * @return Response
     * @throws Throwable
     * @throws Exception
     */
    public function addRepairFeedbackNote(Request $request)
    {
        $data = $request->all();

        if (!isset($data['data']['note']['id'])) {
            $validate = new ReviewFeedback();
            $validate->scene('AddRepairFeedbackNoteData');
            if (!$validate->check($data)) {
                throw_strack_exception($validate->getError(), ErrorCode::REQUEST_PARAM_ERROR);
            }

            $validate->scene('AddShotRepairFeedbackNoteItem');
            if ($data['data']['note']['module_code'] == "shot") {
                foreach ($data['data']['note_relation']['relation_list'] as $relationItem) {
                    if (!$validate->check($relationItem)) {
                        throw_strack_exception($validate->getError(), ErrorCode::REQUEST_PARAM_ERROR);
                    }
                }
            }
        }

        try {
            $req = $this->reviewFeedbackService->addRepairFeedbackNote($data['data']);
            return json(success_response('', $req));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }


    }

    /**
     * 获取受影响的镜头
     * @param Request $request
     * @return Response
     */
    public function getAffectedShotList(Request $request)
    {
        $filter = $request->all()['param']['filter'];
        $req = $this->reviewFeedbackService->getAffectedShotList($filter);
        return json(success_response('', $req));
    }

    /**
     * 获取审核反馈的实体的工序分类
     * @param Request $request
     * @return Response
     */
    public function getEntityReviewFeedbackStepCategoryList(Request $request)
    {
        $param = $request->all()['param'];
        $req = $this->reviewFeedbackService->getEntityReviewFeedbackStepCategoryList($param["filter"]);
        return json(success_response('', $req));
    }

    /**
     * 批量修改反馈
     * @param Request $request
     * @return Response
     * @throws Exception
     */
    public function batchUpdateSameNote(Request $request)
    {
        try {
            $data = $request->all()['data'];
            $req = $this->reviewFeedbackService->batchUpdateSameNote($data);
            return json(success_response('', $req));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 获取实体任务列表
     * @param Request $request
     * @return Response
     */
    public function getEntityTaskList(Request $request)
    {
        try {
            $filter = $request->all()['param']["filter"];
            $req = $this->reviewFeedbackService->getEntityTaskList($filter["entity_id"]);
            return json(success_response('', $req));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 删除审核反馈返修意见
     * @param Request $request
     * @return Response
     * @throws Throwable
     */
    public function deleteRepairFeedbackNote(Request $request)
    {
        try {
            $filter = $request->all()['param']["filter"];
            $req = $this->reviewFeedbackService->deleteRepairFeedbackNote($filter);
            return json(success_response('', $req));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }

    }

    /**
     * 删除审核反馈
     * @param Request $request
     * @return Response
     * @throws Throwable
     */
    public function deleteFeedback(Request $request)
    {
        try {
            $filter = $request->all()['param']["filter"];
            $req = $this->reviewFeedbackService->deleteFeedback($filter);
            return json(success_response('', $req));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 获取审核反馈媒体列表
     * @param Request $request
     * @return Response
     */
    public function getReviewFeedbackMediaList(Request $request)
    {
        try {
            $param = $request->all()['param'];
            $req = $this->reviewFeedbackService->getReviewFeedbackMediaList($param);
            return json(success_response('', $req));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 获取审核反馈资产列表
     * @param Request $request
     * @return Response
     */
    public function getReviewFeedbackAssetList(Request $request)
    {
        $param = $request->all()['param'];
        $req = $this->reviewFeedbackService->getReviewFeedbackAssetList($param);
        return json(success_response('', $req));
    }

    /**
     * 获取审核反馈返修意见详情
     * @param Request $request
     * @return Response
     */
    public function getReviewFeedbackNoteDetail(Request $request)
    {
        $param = $request->all()['param'];
        $noteId = $param['filter']['note_id'];
        $req = $this->reviewFeedbackService->getReviewFeedbackNoteDetail($noteId);
        return json(success_response('', $req));
    }

    /**
     * 发送通知
     * @param Request $request
     * @return Response
     */
    public function sendNotify(Request $request)
    {
        $data = $request->all()['data'];
        $reviewFeedbackId = $data['review_feedback_id'];
        $notifyUserIdList = $data['notify_user_id_list'];
        $confirmLimitTime = $data['confirm_limit_time'];
        $notifyContent = $data['notify_content'];
        try {
            $req = $this->reviewFeedbackService->sendNotify($reviewFeedbackId, $notifyUserIdList, $confirmLimitTime, $notifyContent, $request->getUnionUserId());
            return json(success_response('', $req));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }


    /**
     * 导入审核反馈
     * @param Request $request
     * @return Response
     * @throws Throwable
     * @throws Exception
     */
    public function importRepairFeedbackNote(Request $request)
    {
        $data = $request->all()['data'];

        $validator = new ReviewFeedback();
        if ($data['module_code'] == 'shot') {
            $validator->scene("ImportShotRepairFeedbackNoteList");
        } else {
            $validator->scene("ImportAssetRepairFeedbackNoteList");
        }
        foreach ($data['list'] as $listItem) {
            if (!$validator->check($listItem)) {
                throw_strack_exception($validator->getError(), ErrorCode::REQUEST_PARAM_ERROR);
            }
        }

        try {
            $req = $this->reviewFeedbackService->importRepairFeedbackNote($data['list'], $data['module_code'], $data['project_id'], $data['review_feedback_id']);
            return json(success_response('', $req));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 保存审核反馈计划
     * @param Request $request
     * @return Response|void
     * @throws Throwable
     */
    public function saveReviewFeedbackPlan(Request $request)
    {
        $data = $request->all()['data'];
        try {
            $req = $this->reviewFeedbackService->saveReviewFeedbackPlan($data['review_feedback_id'], $data['episode_id'], $data['step_category_id'], $data['plan_delivery_time']);
            return json(success_response('', $req));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 更新需求单实体计划
     * @param Request $request
     * @return Response|void
     * @throws \Exception
     */
    public function saveReviewFeedbackEntityPlan(Request $request)
    {
        try {
            $data = $request->all()['data'];
            $req = $this->reviewFeedbackService->saveReviewFeedbackEntityPlan($data['review_feedback_entity_id'], $data['step_category_id'], $data['plan_delivery_time']);
            return json(success_response('', $req));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }


    /**
     * 批量更新需求单实体计划
     * @param Request $request
     * @return Response|void
     */
    public function batchSaveReviewFeedbackEntityPlan(Request $request)
    {
        try {
            $data = $request->all()['data'];
            $req = $this->reviewFeedbackService->batchSaveReviewFeedbackEntityPlan($data['review_feedback_entity_ids'], $data['step_category_id'], $data['plan_delivery_time']);
            return json(success_response('', $req));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 查询任务是否限制添加返修项
     * @param Request $request
     * @return Response|void
     */
    public function restrictionAddRepairFeedbackNote(Request $request)
    {
        $param = query_array_construct([
            'task_ids' => 'split_array|array_map,intval'
        ], $request->all()['param']);
        try {
            $res = $this->reviewFeedbackService->restrictionAddRepairFeedbackNote($param['task_ids']);
            return json(success_response('', $res));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 获取所有审核反馈实体表ID
     * @param Request $request
     * @return Response
     */
    public function getReviewFeedbackEntityIds(Request $request)
    {
        $param = $request->all()['param'];
        $initialEpisodeCode = $param['initial_episode_code'] ?? [];

        $req = $this->reviewFeedbackService->getReviewFeedbackEntityIds($param['review_feedback_id'], $param['module_code'], $initialEpisodeCode);
        return json(success_response('', $req));
    }

}
